import { createRouter, createWebHashHistory } from 'vue-router'
import {getToken} from '../utils/auth'
const routes = [
  {
    name: 'notFound',
    path: '/:path(.*)+',
    redirect: {
      name: 'login'
    }
  }, 
  {
    path: '/address',
    component:() => import('@/components/layout/index.vue'),
    redirect: 'address',
    children: [ 
      {
        name: 'address',
        path: '/address',
        component: () => import('@/view/address/index.vue'),
        meta: {
          title: '地址列表'
        }
      }, {
        name: 'addressEdit',
        path: '/addressEdit',
        component: () => import('@/view/address/edit.vue'),
        meta: {
          title: '编辑地址'
        }
      },
    ]
  },
  {
    path: '/user',
    component:() => import('@/components/layout/index.vue'),
    redirect: 'user',
    children: [ 
      {
        name: 'user',
        path: '/user',
        component: () => import('@/view/user/index.vue'),
        meta: {
          title: '会员中心'
        }
      }, 
      {
        name: 'excelOpt',
        path: '/excelOpt',
        component: () => import('@/view/excelOpt/index.vue'),
        meta: {
          title: 'Excel操作'
        }
      },{
        name: 'file',
        path: '/file',
        component: () => import('@/view/file/index.vue'),
        meta: {
          title: '文件上传'
        }
      },
      {
        name: 'fileLoad',
        path: '/fileLoad',
        component: () => import('@/view/file/loadFileIndex.vue'),
        meta: {
          title: '文件读取'
        }
      },
    ]
  },
  {
    path: '/',
    component: () => import('@/components/layout/index.vue'),
    hidden: true,
    redirect: 'home',
    children: [
      {
        path: '/home',
        component: () => import('@/view/home/index.vue'),
        name: 'home',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      },  
      {
        name: 'cart',
        path: '/cart',
        component: () => import('@/view/cart/index.vue'),
        meta: {
          title: '购物车'
        }
      },
      {
        name: 'goods',
        path: '/goods',
        component: () => import('@/view/goods/index.vue'),
        meta: {
          title: '商品列表'
        },
        children: [
          {
            name: 'goodsdetail',
            path: '/detail',
            component: () => import('@/view/goods/detail.vue'),
            meta: {
              title: '商品详情'
            }
          },
        ]
      },  
      {
        name: 'test',
        path: '/test',
        component: () => import('@/view/test/index.vue'),
        meta: {
          title: 'test页'
        }
      }, 
    ]
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/view/login.vue'),
    meta: {
      title: '登录'
    }
  }
]

// 防止连续点击多次路由报错
//........
 
const router = createRouter({
  routes,
  history: createWebHashHistory()
})

router.beforeEach((to, from, next) => {
  const title = to?.meta?.title
  if (title) {
    document.title = title as string
  }
  if(getToken() || to.path == '/login'){
    next()
  }
  else {
    next("/login")
  }
})

export default router
